package br.com.extrajudicial.dao;

import java.util.List;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import br.com.caelum.vraptor.ioc.Component;
import br.com.extrajudicial.models.Address;
import br.com.extrajudicial.models.UserSession;

@Component
public class AddressDao extends GenericDao<Address>{
	
	@Autowired private UserSession userSession;
	
	@SuppressWarnings("unchecked")
	public List<Address> search(String query) {
		return this.getSession().createCriteria(Address.class)
			.add(Restrictions.disjunction()
				.add(Restrictions.ilike("address", query, MatchMode.ANYWHERE))
				.add(Restrictions.ilike("city", query, MatchMode.ANYWHERE))
				.add(Restrictions.ilike("state", query, MatchMode.ANYWHERE))
				.add(Restrictions.ilike("country", query, MatchMode.ANYWHERE))
				.add(Restrictions.ilike("zipcode", query, MatchMode.ANYWHERE)))
			.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Address> listCurrentUserAddresses() {
		return this.getSession().createCriteria(Address.class)
			.add(Restrictions.eq("user", this.userSession.getCurrentUser()))
			.list();
	}

}
